<?php
// +----------------------------------------------------------------------
// | 有小象汇 自研正版源码 赋能开发者
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2023 http://www.uxphp.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 有小象汇并不是自由软件，未经许可不能去掉有小象汇相关版权
// +----------------------------------------------------------------------
// | Author:  有小象汇
// +----------------------------------------------------------------------

namespace app\dao\car;

use app\dao\BaseDao;
use app\model\car\BaseSuggest;

class BaseSuggestDao extends BaseDao
{
    public function setModel(): string
    {
        // TODO: Implement setModel() method.
        return BaseSuggest::class;
    }

    /**
     * 获取列表
     * @param array $where
     * @param int $page
     * @param int $limit
     * @return array
     */
    public function getList(array $where=[],int $page=0,int $limit=0) {
        $model = $this->getModel()->alias('s')->where('s.is_del','=',0);
        $model = $model->field('s.*, u.uid,u.real_name');
        $model = $model->join('user u','s.uid = u.uid','left');
        $model = $this->setSearch($model,$where);
        $count = $model->count();
        $list = $model->order('s.id desc')->page($page, $limit)->select()->toArray();
        return compact('count','list');
    }

    /**
     * 搜索条件
     * @param $model
     * @param $where
     * @return mixed
     */
    public function setSearch($model,$where){
        if($where['status'] !== '') {
            $model->where('s.status','=',$where['status']);
        }
        if(isset($where['field_key'])&&$where['field_key']){
            if($where['field_key'] == 'name'){
                $model->where('u.real_name','like',"%{$where['keyword']}%");
            }
            if($where['field_key'] == 'content'){
                $model->where('s.content','like',"%{$where['keyword']}%");
            }
        }
        return $model;
    }

    /**
     * 删除
     * @param int $id
     * @return array
     */
    public function del (int $id) {
        $del = $this->getModel()->where('id',$id)->useSoftDelete('delete_time',time())->delete();
        $this->getModel()->where('id',$id)->useSoftDelete('is_del',1)->delete();
        return ['删除成功',$del];
    }
}
